package me.xiaoyan.android.tuition.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Handler;

public class AppManager extends Application {
	private List<Activity> activityList = new ArrayList<Activity>();
	private List<String> childActivitys = new ArrayList<String>();
	private Map<String, Object> appDatas = new HashMap<String, Object>();
	
	private Context context;
	private Handler handler;
	

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	public Handler getHandler() {
		return handler;
	}

	public void setHandler(Handler handler) {
		this.handler = handler;
	}

	private static AppManager instance;

	private AppManager() {

	}

	public Object getAppDatas(String key) {
		return appDatas.get(key);
	}

	public boolean hasAppDatas(String key) {
		return appDatas.containsKey(key);
	}

	public void setAppDatas(String key, Object data) {
		this.appDatas.put(key, data);
	}
	
	public void saveAppDatas(String key, Object data) {
		this.appDatas.put(key, data);
	}
	
	public void delAppDatas(String key) {
		this.appDatas.remove(key);
	}

	public static AppManager getInstance() {
		if (instance == null) {
			instance = new AppManager();
		}
		return instance;
	}

	public void addActivity(Activity activity) {
		activityList.add(activity);
		childActivitys = new ArrayList<String>();
	}

	public void addChildActivity(String id) {
		childActivitys.add(id);
	}

	public List<Activity> getActivitys() {
		return activityList;
	}

	public List<String> getChildActivities() {
		return childActivitys;
	}

	public void exit() {
		for (Activity activity : activityList) {
			if (!activity.isFinishing()) {
				activity.finish();
			}
		}
		int id = android.os.Process.myPid();
		if (id != 0) {
			android.os.Process.killProcess(id);
		}
	}
}
